/*
 * DemoCameraDevice.cpp
 *
 *  Created on: 2018年10月4日
 *      Author: zhengboyuan
 */

#include "stdafx.h"
#include "DemoCameraDevice.h"
#include "TRandom.h"


DemoCameraDevice::DemoCameraDevice()
{
	comn::Random::makeSeed();
}

DemoCameraDevice::~DemoCameraDevice()
{
	close();
}


bool DemoCameraDevice::open(const Camera& camera)
{
	m_camera = camera;

	if (!isRunning())
	{
		start();
	}

	return true;
}

void DemoCameraDevice::close()
{
	if (isRunning())
	{
		stop();
	}
}

bool DemoCameraDevice::isConnected()
{
	return true;
}

bool DemoCameraDevice::reconnect()
{
    return true;
}

int DemoCameraDevice::run()
{
	int count = 0;

	while (!m_canExit)
	{
		m_event.timedwait(1000);

		if (m_canExit)
		{
			break;
		}

		count ++;

		int enterNum = comn::Random::makeNumber(1, 10);
		int leaveNum = 0;

		if (count % 2 == 0)
		{
			leaveNum = comn::Random::makeNumber(1, enterNum);
		}

		{
			comn::AutoCritSec lock(m_cs);
			m_flowCount.enterNum += enterNum;
			m_flowCount.leaveNum += leaveNum;
		}

		fireFlowCount(m_flowCount);

		if (count % 7 == 0)
		{
			fireDeviceStatus(false);
		}
		else if (count % 11 == 0)
		{
			fireDeviceStatus(true);
		}
	}
	return 0;
}

void DemoCameraDevice::doStop()
{
	m_event.post();
}